
/////////////////////////////////////////////////////////
//	File Name : "CGame.cpp"
//	Author Name : ZombieHead
//	Purpose : To contain all game related code
/////////////////////////////////////////////////////////

#include "CGame.h"
#include "CAnimationManager.h"
#include "CMainMenuState.h"
//#include "CIntroState.h"
//#include "COutroState.h"
//#include "CGamePlayState.h"
//#include "COptionsState.h"
//#include "CSaveLoadState.h"
//#include "CProfileSelectState.h"
//#include "CArcadeControls.h"
//#include "CProfileManager.h"
//#include "CCreditsState.h"
//#include "CLoadingState.h"
//#include "CWaterManager.h"

CGame::CGame()
{
	m_pD3D				=	NULL;
	m_pTM				=	NULL;
	m_pFM				=	NULL;
	m_pDI				=	NULL;
	//m_pPM				=	NULL;
	m_ScreenWidth	= 0;
	m_ScreenHeight	= 0;
	//m_pCurrentLevel		=	NULL;
	m_nScore			= 0;
	m_nBrightness = 255;
}

CGame::~CGame()
{

}

CGame::CGame(const CGame& game)
{

}

CGame& CGame::operator =(const CGame& game)
{
	return *this;
}

CGame* CGame::GetInstance()
{
	static CGame instance;
	return &instance;
}

void CGame::Initialize(HWND hWnd, HINSTANCE hInstance, int nScreenWidth, int nScreenHeight, bool bIsWindowed, bool IsArcade)
{
	// Get pointers to singletons

	m_pD3D	=	CSGD_Direct3D::GetInstance();
	m_pTM	=	CSGD_TextureManager::GetInstance();
	m_pFM	=	CSGD_FModManager::GetInstance();
	m_pDI	=	CSGD_DirectInput::GetInstance();
	//m_pPM	=	CProfileManager::GetInstance();
	m_pAM	=	CAnimationManager::GetInstance();
	// Initialize Singletons
	m_hWnd = hWnd;
	m_pD3D->InitDirect3D(hWnd, nScreenWidth, nScreenHeight, bIsWindowed, false);
	m_pTM->InitTextureManager( m_pD3D->GetDirect3DDevice(), m_pD3D->GetSprite());
	m_pFM->InitFModManager(hWnd);
	m_pDI->InitDirectInput(hWnd, hInstance, DI_KEYBOARD /*| DI_MOUSE*/, 0);
	m_pDI->InitDirectInput(hWnd, hInstance, DI_JOYSTICKS /*| DI_MOUSE*/, 0);
	m_pDI->InitDirectInput(hWnd, hInstance, DI_MOUSE /*| DI_MOUSE*/, 0);
	//m_pPM->Initialize();

	XInputEnable(true);
	XINPUT_STATE xState;
	DWORD erro = XInputGetState(0, &xState);
	if(erro == ERROR_DEVICE_NOT_CONNECTED)
	{
		m_bGamePad = false;
	}
	else
		m_bGamePad = true;
	m_RenderRect.top = 0;
	m_RenderRect.left = 0;
	m_RenderRect.bottom = nScreenHeight;
	m_RenderRect.right = nScreenWidth;
	
	m_pAM->LoadAnim("resource\\lambda\\stand.mcf");
	m_pAM->LoadAnim("resource\\lambda\\Youmu.mcf");
	//m_pAM->LoadAnim("resource\\assets\\whip.mcf");
	//m_pAM->LoadAnim("resource\\assets\\mummy.mcf");
	//m_pAM->LoadAnim("resource\\assets\\bat.mcf");
	//m_pAM->LoadAnim("resource\\assets\\pickups.mcf");
	//m_pAM->LoadAnim("resource\\assets\\spider.mcf");
	//m_pAM->LoadAnim("resource\\assets\\rat.mcf");
	//m_pAM->LoadAnim("resource\\assets\\snake.mcf");
	//m_pAM->LoadAnim("resource\\assets\\speartrap.mcf");
	//m_pAM->LoadAnim("resource\\assets\\flametrap.mcf");
	//m_pAM->LoadAnim("resource\\assets\\door.mcf");
	//m_pAM->LoadAnim("resource\\assets\\web.mcf");
	//m_pAM->LoadAnim("resource\\assets\\grapplepoint.mcf");
	//m_pAM->LoadAnim("resource\\assets\\lever.mcf");
	//m_pAM->LoadAnim("resource\\assets\\jumppad.mcf");
	//m_pAM->LoadAnim("resource\\assets\\seekarrow.mcf");
	//m_pAM->LoadAnim("resource\\assets\\loading.mcf");

	// Initialize States
	// Initialize States
	//if(IsArcade)
	//	m_pCurrState = CArcadeControlsState::GetInstance();
	//else
		m_pCurrState = CMainMenuState::GetInstance();

	
	// Set GameState
	AddState(m_pCurrState);

	m_ScreenWidth = nScreenWidth;
	m_ScreenHeight = nScreenHeight;
	m_bFullScreen = bIsWindowed;
	m_fSfxVol = .5f;
	m_fBGM = .50f;
	m_fPan = 0.0f;

	m_StopWatch.Start();
	//CThreadSystem::GetInstance()->InitSyncSystem();
	//m_nWaterLockObJ = CThreadSystem::GetInstance()->CreateChangeObject();
	//m_nLoadingLockObj = CThreadSystem::GetInstance()->CreateChangeObject();
}

bool CGame::Main()
{
	////////////////////////////////////////
	//GameTime
	m_fElapsedTime = (float)m_StopWatch.GetElapsedTime();
	m_StopWatch.Reset();
	////////////////////////////////////////


	// Input
	if( Input() == false )
		return false;

	// Update
	Update(m_fElapsedTime);

	// Render
	Render();

	return true;
}

bool CGame::Input()
{
	m_pDI->ReadDevices();	// only call once per frame
	static bool windowed = true;
	if(((m_pDI->KeyDown(DIK_LALT) && m_pDI->KeyPressed(DIK_RETURN))))
	{
		m_bFullScreen = !m_bFullScreen;
		m_pD3D->ChangeDisplayParam(m_ScreenWidth, m_ScreenHeight, m_bFullScreen);
	}
	else if(((m_pDI->KeyDown(DIK_RALT) && m_pDI->KeyPressed(DIK_RETURN))))
	{
		m_bFullScreen = !m_bFullScreen;
		m_pD3D->ChangeDisplayParam(m_ScreenWidth, m_ScreenHeight, m_bFullScreen);
	}
	if(m_pGameState.back()->Input() == false)
		return false;
	return true;
}

void CGame::Update(float fElapsedTime)
{
	if(fElapsedTime >= 0.7f)
		fElapsedTime = 0.1f;
	m_pFM->Update();
	size_t activeStateIndex = m_pGameState.size()-1;
	m_pGameState[activeStateIndex]->Update(fElapsedTime);
}

void CGame::Render()
{
	m_pD3D->Clear(0, 0, 0);
	m_pD3D->DeviceBegin();
	m_pD3D->SpriteBegin();

	for(size_t i = 0; i < m_pGameState.size(); i++)
		m_pGameState[i]->Render(m_nBrightness);
	//m_pGameState.back()->Render ();

	m_pD3D->SpriteEnd();
	m_pD3D->DeviceEnd();
	m_pD3D->Present();
}

void CGame::ChangeState(IGameState* state)
{
	ClearAllStates();
	
	if(state)
		AddState(state);
}

void CGame::AddState(IGameState* state)
{
	m_pGameState.push_back(state);
	state->Enter();
}

void CGame::RemoveStateLastState()
{
	m_pGameState.back()->Exit();
	m_pGameState.pop_back();
}

void CGame::ClearAllStates()
{
	for(size_t i = 0; i < m_pGameState.size(); i++)
	{
		if(m_pGameState[i])
		{
			m_pGameState[i]->Exit();
			m_pGameState[i] = NULL;
		}
	}

	m_pGameState.clear();
}

bool CGame::IsLastState()
{
	if(m_pGameState.size() == 1)
		return true;
	else
		return false;
}

void CGame::Shutdown()
{
	//CThreadSystem::GetInstance()->CloseSyncSystem();
	//CWaterManager::GetInstance()->DeleteInstance();
	ChangeState(NULL);
	//CThreadSystem::GetInstance()->CloseSyncSystem();
	//m_pAM->Shutdown();
	//m_pAM->DeleteInstance();
	//int active = CProfileManager::GetInstance()->GetActiveProfile();
	//CProfileManager::GetInstance()->GetProfiles()[active]->SetBGM(m_fBGM);
	//CProfileManager::GetInstance()->GetProfiles()[active]->SetPan(m_fPan);
	//CProfileManager::GetInstance()->GetProfiles()[active]->SetSFX(m_fSfxVol);
	//CProfileManager::GetInstance()->SaveProfiles();
	//if(m_pPM)
	//{
	//	m_pPM->Shutdown();
	//	m_pPM = NULL;
	//}

	//if(m_pPM)
	//{
	//	m_pPM->Shutdown();
	//	m_pPM = NULL;
	//}

	// Shutdown in opposite order
	if(m_pDI)
	{
		m_pDI->ShutdownDirectInput();
		m_pDI = NULL;
	}

	if(m_pFM)
	{
		m_pFM->ShutdownFModManager();
		m_pFM = NULL;
	}

	if(m_pTM)
	{
		m_pTM->ShutdownTextureManager();
		m_pTM = NULL;
	}

	if(m_pD3D)
	{
		m_pD3D->ShutdownDirect3D();
		m_pD3D = NULL;
	}
}

void CGame::LoadData()
{
}



